package main

import (
	"github.com/micro/cli/v2"
	"github.com/micro/go-micro/v2"
	"log"
	"runtime"

	"kop/framework/broker"
	"kop/framework/conf"
	"kop/framework/wrapper"
	"kop/libs/database"
	"kop/modules/world_boss/handler"
	"kop/pb/service"
	log2 "kop/util/log"
)

func main() {

	runtime.GOMAXPROCS(runtime.NumCPU())

	conf.Init()
	log2.SetLevel(conf.Log.Level)

	var srv = micro.NewService(
		micro.Name("go.micro.srv.world_boss"),
		micro.WrapHandler(wrapper.NewLogWrapper),
		micro.Action(func(c *cli.Context) error {
			return broker.Init(c.String("broker_address"))
		}),
		micro.AfterStart(
			func() error {
				log.Println("启动世界BOSS服务器.")
				return nil
			}),
	)

	database.DB = database.Open(conf.DataBase.Dsn, conf.DataBase.MaxIdle, conf.DataBase.MaxOpen, conf.DataBase.Log)
	defer database.DB.Close()

	var h = handler.NewWorldBossServiceHandler(database.DB)

	// Register handler
	_ = service.RegisterWorldBossServiceHandler(srv.Server(), &h)

	// Initialise service
	srv.Init()

	if err := srv.Run(); err != nil {
		panic(err)
	}
}
